package com.de.juxfux.catfrags;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.de.juxfux.JfTextView;
import com.de.juxfux.R;
import com.de.juxfux.db.DbTableJuxdata;
import com.de.juxfux.db.Juxdata;
import com.de.juxfux.settings.SettingsActivity;


public class JuxCursorAdapter extends CursorAdapter {
	public static final String TAG = JuxCursorAdapter.class.getSimpleName();
	private int layout;
	private Cursor cursor;
	
	public JuxCursorAdapter(Context context, int layout, Cursor cursor) {
		super(context, cursor, 0);
		this.layout = layout;
		this.cursor = cursor;
	}

	
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView idView = (TextView) view.findViewById(R.id.jflistview_textview_id);
		TextView typeView = (TextView) view.findViewById(R.id.jflistview_textview_type);
		TextView catView = (TextView) view.findViewById(R.id.jflistview_textview_category);
		JfTextView textView = (JfTextView) view.findViewById(R.id.jflistview_textview_text);
		
		idView.setText(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_ID)));
		typeView.setText(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TYPE)));
		catView.setText(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_CAT)));
		textView.setText(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TEXT)).trim());

		JfTextView tagsView = (JfTextView) view.findViewById(R.id.jflistview_textview_tags);
		if (SettingsActivity.isShowTagsActive(context)) {
			tagsView.setTags(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TAGS)));
			tagsView.setVisibility(View.VISIBLE);
		} else {
			tagsView.setVisibility(View.GONE);
		}
		
//		TODO: Logs hier rauswerfen
//		Log.d(TAG, "(bindView) - ID="+cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_ID)) + ":  TAGS="
//				+ cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TAGS)));
		
//		String text = cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TEXT));
//		String logText = "bindView(...) - " +
//				"ID=" + cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_ID)) +
//				", TAGS=" + cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TAGS)) +
//				", TEXT=" + text.substring(0, (text.length() > 20) ? 20 : text.length());
//		Log.d(TAG, logText);
	}

	
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
	    final View view = LayoutInflater.from(context).inflate(layout, parent, false);
	    return view;
	}
	

	@Override
	public void changeCursor(Cursor cursor) {
		this.cursor = cursor;
		super.changeCursor(cursor);
	}
	
	
	public ArrayList<Juxdata> getCursorDataAsList() {
	    if (cursor != null && cursor.moveToFirst()) {
	    	ArrayList<Juxdata> juxdataList = new ArrayList<Juxdata>();
	    	
	    	for (int i = 0; i < cursor.getCount(); i++) {
	        	Juxdata juxdata = new Juxdata();
	        	juxdata.setId(cursor.getInt(cursor.getColumnIndex(DbTableJuxdata.COLUMN_ID)));
	        	juxdata.setType(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TYPE)));
	        	juxdata.setCategory(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_CAT)));
	        	juxdata.setTags(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TAGS)));
	        	juxdata.setText(cursor.getString(cursor.getColumnIndex(DbTableJuxdata.COLUMN_TEXT)));
	        	juxdataList.add(juxdata);
	            cursor.moveToNext();
	        }
	    	
	    	return juxdataList;
	    }
		return null;
	}

}
